package com.biqee.service.bq.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.biqee.dao.BqQueryRelationDAO;
import com.biqee.dao.BqTuanDAO;
import com.biqee.model.common.PageInfoObject;
import com.biqee.model.db.BqQuery;
import com.biqee.model.db.BqQueryRelation;
import com.biqee.model.db.BqTuan;
import com.biqee.model.db.TbItem;
import com.biqee.model.page.ItemVO;
import com.biqee.service.bq.BqQueryRelationService;
import com.biqee.util.BigDecimalUtil;

public class BqQueryRelationServiceImpl implements BqQueryRelationService {
	private static final Logger logger = Logger.getLogger(BqQueryRelationServiceImpl.class);
	
	private BqQueryRelationDAO bqQueryRelationDAO;
	
	private BqTuanDAO bqTuanDAO ;
	
	@Override
	public void save(BqQueryRelation record){
		if(record.getId()==null){
			record.setIsDeleted("0");
			record.setGmtCreate(new Date());
			record.setGmtModified(new Date());
			bqQueryRelationDAO.insert(record);
		}else{
			bqQueryRelationDAO.updateByPrimaryKeySelective(record);
		}
		
		
	}
	
	@Override
	public void clearRelationByQueryId(Integer queryId) {
		bqQueryRelationDAO.deleteByQueryId(queryId);
	}

	@Override
	public void createRelation(BqQuery bqQuery, List<TbItem> list) {
		Integer tuanId= bqQuery.getTuanId();
		Integer queryId = bqQuery.getId();
		BqTuan bqTuan = bqTuanDAO.read(tuanId);

		for(TbItem tbItem :list){
			BqQueryRelation relation = new BqQueryRelation();
			
			relation.setTuanId(tuanId);
			relation.setRefId(tbItem.getId());
			BqQueryRelation dBRelation  = bqQueryRelationDAO.getBySelective(relation);
			if(dBRelation==null){
				dBRelation = relation;
			}else if(-1==dBRelation.getQueryId()){
				continue ;
			}
			dBRelation.setQueryId(queryId);
			if (tbItem.getMinPrice() != null && bqTuan != null
					&& bqTuan.getTuanPrice() != null) {
				try {
					BigDecimal itemPrice = new BigDecimal(tbItem.getMinPrice());
					if(tbItem.getPostFee()!=null){
						itemPrice = BigDecimalUtil.add(itemPrice, new BigDecimal(tbItem.getPostFee()));
					}
					BigDecimal distance =	BigDecimalUtil.subtract(itemPrice, new BigDecimal(bqTuan.getTuanPrice()));
					dBRelation.setDistancePrice(distance.doubleValue());	
				} catch (Exception ex) {
					logger.error("error: ", ex);
				}
				
			}
			this.save(dBRelation);
		}
	}

	public void setBqQueryRelationDAO(BqQueryRelationDAO bqQueryRelationDAO) {
		this.bqQueryRelationDAO = bqQueryRelationDAO;
	}

	public void setBqTuanDAO(BqTuanDAO bqTuanDAO) {
		this.bqTuanDAO = bqTuanDAO;
	}

	@Override
	public List<ItemVO> listRelationItemsByPage(PageInfoObject page) {
		int count = this.bqQueryRelationDAO.countRelationItems(page.getParams());
		page.setCount(count);
		return this.bqQueryRelationDAO.listRelationItemsByPage(page);
	}

	@Override
	public List<Integer> readAllTbItemIdsByBqTuanId(Integer bqTuanId) {

		return bqQueryRelationDAO.readAllTbItemIdsByBqTuanId(bqTuanId);
	}


	@Override
	public boolean delete(Integer bqQueryRelationId) {
		return bqQueryRelationDAO.deleteByPrimaryKey(bqQueryRelationId)==1;
	}


	@Override
	public Integer getTbItemId(Integer bqTuanId) {
		return bqQueryRelationDAO.getTbItemIdByBqTuanId(bqTuanId);
	}

	
}
